﻿$(document).ready(function () {
    if ($("#result").html() == "操作成功" || $("#result").html() == "修改成功") {

        //关闭当前对话框并刷新父页面
        $(window.top.document).find("#refreshBtn").click();
        window.top.art.dialog.get('operationmanagevehicleoilboxmanage').close();
    }

    //显示不同的油箱类型对应的设置框
    $("#typeContainer input[type='radio']").each(function (index) {
        if ($(this).attr("checked") == "checked") {
            $("form>div").hide();
            $("form>div").eq(index).show();
        }
    });

    $("#typeContainer input[type='radio']").change(function () {
        if ($(this).attr("checked") == "checked") {
            var index = $(this).index("#typeContainer input[type='radio']");
            $("form>div").hide();
            $("form>div").eq(index).show();
        }
    });

    var validators = new Array();
    var validator = $("form").eq(0).validate({
        
        rules: {
            oilCapacity:
            {
                required: true,
                number: true
            },
            oilHeight:
            {
                required: true,
                number: true
            }
        }
    });
    validators.push(validator);
    
    validator = $("form").eq(1).validate({
        //errorPlacement: function (error, element) {
        //    error.appendTo(element.parent());
        //},
        rules: {
            oilCapacity:
            {
                required: true,
                number: true
            },
            oilWidth:
            {
                required: true,
                number: true
            },
            oilLength:
            {
                required: true,
                number: true
            },
            oilHeight:
            {
                required: true,
                number: true
            }
        }
    });
    validators.push(validator);

    validator = $("form").eq(2).validate({
        //errorPlacement: function (error, element) {
        //    error.appendTo(element.parent());
        //},
        rules: {
            oilCapacity:
            {
                required: true,
                number: true
            },
            oilHeight:
            {
                required: true,
                number: true
            }
        }
    });
    validators.push(validator);

    validator = $("form").eq(3).validate({
        //errorPlacement: function (error, element) {
        //    error.appendTo(element.parent());
        //},
        rules: {
            oilCapacity:
            {
                required: true,
                number: true
            },
            oilWidth:
            {
                required: true,
                number: true
            },
            oilLength:
            {
                required: true,
                number: true
            },
            oilHeight:
            {
                required: true,
                number: true
            }
        }
    });
    validators.push(validator);

    //刚加载时对采样数据的解析和显示
    var str = $("#sampleData").val();
    var array = str.split(";");
    if (array.length > 1) {
        $("#sampledatacount").val(Number(array.length / 2));
        $("#clearSampleData").show();
        var sampleDataStr = "";
        for (var i = 0; i < array.length; i += 2) {
            sampleDataStr += ("[" + array[i] + "," + array[i + 1] + "];");
        }
        if (sampleDataStr.length > 0) {
            sampleDataStr = sampleDataStr.substr(0, sampleDataStr.length - 1);
        }
        $("#sampleDataShow").html(sampleDataStr);
    }


    $("#addSampleData").click(function () {
        if ($("#samplex").val() == "" || isNaN($("#samplex").val())) {
            artDialogAlert("油量必须输入数字");
            $("#samplex").focus();
            return false;
        }
        if ($("#sampley").val() == "" || isNaN($("#sampley").val())) {
            artDialogAlert("参数必须输入数字");
            $("#sampley").focus();
            return false;
        }

        if (Number($("#sampledatacount").val()) >= 10) {
            artDialogAlert("采样值已达到10组");
            return false;
        }

        $("#sampledatacount").val(Number($("#sampledatacount").val()) + 1);
        $("#sampleData").val($("#sampleData").val() + $("#samplex").val() + ";" + $("#sampley").val() + ";");
        var sampleDataStr = $("#sampleDataShow").html();
        if (sampleDataStr.length > 0) {
            sampleDataStr += ";";
        }
        sampleDataStr += ("[" + $("#samplex").val() + "," + $("#sampley").val() + "]");
        $("#sampleDataShow").html(sampleDataStr);

        $("#samplex").val("");
        $("#sampley").val("");
        if (array.length > 0) {
            $("#clearSampleData").show();
        }
    });

    $("#clearSampleData").click(function () {
        $("#sampleDataShow").html("");
        $("#sampleData").val("");
        $("#sampledatacount").val("0");
        $("#clearSampleData").hide();
    });

    $("#confirmBtn").click(function () {
        var index = $("#typeContainer input[type='radio']:checked").index("#typeContainer input[type='radio']");
        if (index < 4 && validators[index].form()) {
            $("form")[index].submit();
        }
        else if (index == 4) {
            if (Number($("#sampledatacount").val()) < 3) {
                artDialogAlert("请至少填写3组采样值");
                return false;
            }
            $("form")[index].submit();
        }
    });

    //$("form").submit(function () {
    //    alert("submited");
    //});

    //以下是数据的自动填充，即当填写了某些数据项之后，自动生成其他项

    //标准立方体
    $("#type1volumn").change(function () {
        var val = $(this).val();
        if (val == "" || val == null) {
            $("#type1height").val("");
        }
        else {
            var val1=(Math.pow(val*1000, 1 / 3)).toString();
            $("#type1height").val(val1.substring(0,val1.indexOf('.')+4));
        }
    });


    //长方体
    $("#type2volumn").change(function () {
        var val = $("#type2volumn").val();
        var val1 = $("#type2length").val();
        var val2 = $("#type2width").val();
        if (val == "" || val == null || val1 == "" || val1 == null ||val2 == "" || val2 == null) {
            $("#type2height").val("");
            return true;
        }
        else {
            var val3 = (val*1000/val1/val2).toString();
            $("#type2height").val(val3.substring(0, val3.indexOf('.') + 4));
        }
    });
    $("#type2length").change(function () {
        var val = $("#type2volumn").val();
        var val1 = $("#type2length").val();
        var val2 = $("#type2width").val();
        if (val == "" || val == null || val1 == "" || val1 == null || val2 == "" || val2 == null) {
            $("#type2height").val("");
            return true;
        }
        else {
            var val3 = (val * 1000 / val1 / val2).toString();
            $("#type2height").val(val3.substring(0, val3.indexOf('.') + 4));
        }
    });
    $("#type2width").change(function () {
        var val = $("#type2volumn").val();
        var val1 = $("#type2length").val();
        var val2 = $("#type2width").val();
        if (val == "" || val == null || val1 == "" || val1 == null || val2 == "" || val2 == null) {
            $("#type2height").val("");
            return true;
        }
        else {
            var val3 = (val * 1000 / val1 / val2).toString();
            $("#type2height").val(val3.substring(0, val3.indexOf('.') + 4));
        }
    });

    //D型
    $("#type4volumn").change(function () {
        var val = $("#type4volumn").val();
        var val1 = $("#sectiondiameter").val();
        var val2 = $("#type4length").val();
        if (val == "" || val == null || val1 == "" || val1 == null || val2 == "" || val2 == null) {
            $("#type4width").val("");
            return true;
        }
        else {
            var val3 = ((8000*val)/(8*val1*val2+Math.PI*Math.pow(val1,2))).toString();
            $("#type4width").val(val3.substring(0, val3.indexOf('.') + 4));
        }
    });
    $("#sectiondiameter").change(function () {
        var val = $("#type4volumn").val();
        var val1 = $("#sectiondiameter").val();
        var val2 = $("#type4length").val();
        if (val == "" || val == null || val1 == "" || val1 == null || val2 == "" || val2 == null) {
            $("#type4width").val("");
            return true;
        }
        else {
            var val3 = ((8000 * val) / (8 * val1 * val2 + Math.PI * Math.pow(val1, 2))).toString();
            $("#type4width").val(val3.substring(0, val3.indexOf('.') + 4));
        }
    });
    $("#type4length").change(function () {
        var val = $("#type4volumn").val();
        var val1 = $("#sectiondiameter").val();
        var val2 = $("#type4length").val();
        if (val == "" || val == null || val1 == "" || val1 == null || val2 == "" || val2 == null) {
            $("#type4width").val("");
            return true;
        }
        else {
            var val3 = ((8000 * val) / (8 * val1 * val2 + Math.PI * Math.pow(val1, 2))).toString();
            $("#type4width").val(val3.substring(0, val3.indexOf('.') + 4));
        }
    });

});